package com.example.UI;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.ImageView;

/**
 * Created by Administrator on 2017/1/10.
 */

public class Myimageview extends ImageView {
    private Bitmap bitmap;
    private Paint paint;
    private int bitmapheight = 180;
    private int bitmapwidth  = 180;
    private PorterDuffXfermode porterDuffXfermode;
    private static final PorterDuff.Mode MODE = PorterDuff.Mode.SRC_IN;
    public Myimageview(Context context) {
        this(context,null,0);
    }

    public Myimageview(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public Myimageview(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }
    public void getbitmap(Bitmap biamap){
        this.bitmap = biamap;
        invalidate();
    }

    private void init() {
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(Color.RED);
        //创建一个PorterDuffXfermode对象
        porterDuffXfermode = new PorterDuffXfermode(MODE);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        BitmapDrawable bd = (BitmapDrawable) getDrawable();
//        if(bd!=null){
//
//            bitmap = bd.getBitmap();
//            bd.setAlpha(1);
        if(bitmap!=null){
            int sc = canvas.saveLayer(-300,-300,500,500,null, Canvas.MATRIX_SAVE_FLAG);
            //先绘制扇形
            RectF rect = new RectF(-300,-300,300,300);
            canvas.drawArc(rect,0,90,true,paint);
            //在绘制要显示图片
            paint.setXfermode(porterDuffXfermode);
            canvas.drawBitmap(bitmap,0,0,paint);
            paint.setXfermode(null);
            canvas.restoreToCount(sc);

        }


//        }




    }
}
